home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 5.0 KB | 239 lines |
- package symantec.itools.awt;
-
- import java.awt.Image;
-
- // 02/10/97 RKM Added data object
- // 02/27/97 RKM Merged in accessors for getParent, getChild, & getSibling
-
- /**
- * This is a single node in the TreeView panel. It can have both text and/or an
- * image.
- * @see TreeView
- */
- public class TreeNode
- {
- TreeNode sibling;
- TreeNode child;
- TreeNode parent;
- String text;
- Image collapsedImage;
- Image expandedImage;
- int numberOfChildren;
- Object dataObject;
-
- int depth = -1;
- boolean isExpanded = false;
-
- //constructors
-
- /**
- * Constructs a TreeNode with a given text label.
- * @param text the text to display for this node
- */
- public TreeNode(String text)
- {
- this(text, null, null);
- }
-
- /**
- * Constructs a TreeNode with a given text label, and collapsed and
- * expanded images.
- * @param text the text to display for this node
- * @param collapsedImage the image to use when this node is collapsed, hiding
- * all of its child nodes
- * @param expandedImage the image to use when this node is expanded, showing
- * all of its child nodes
- */
- public TreeNode(String text, Image collapsedImage, Image expandedImage)
- {
- this.text = text;
- this.sibling = null;
- this.child = null;
- this.collapsedImage = collapsedImage;
- this.expandedImage = expandedImage;
- this.numberOfChildren = 0;
- this.dataObject = null;
- }
-
- /**
- * Notes the current depth of this node.
- * @param depth
- * @see #getDepth
- */
- void setDepth(int depth)
- {
- this.depth = depth;
- }
-
- /**
- * Gets the depth of this node as previously noted.
- * @return the depth of this node
- * @see #setDepth
- */
- public int getDepth()
- {
- return depth;
- }
-
- /**
- * Gets the expanded state of this node. A node is expanded
- * if its child nodes are visible.
- * @return true if the node is expanded, false if collapsed
- */
- public boolean isExpanded()
- {
- return isExpanded;
- }
-
- /**
- * Gets whether this node is expandable. A node is expandable
- * if it has one or more child nodes.
- * @return true if the node is expandable, false if not
- */
- public boolean isExpandable()
- {
- return (child!=null);
- }
-
- /**
- * Notes that this node is expanded, if it is expandable.
- */
- public void expand()
- {
- if (isExpandable())
- {
- isExpanded=true;
- }
- }
-
- /**
- * Notes that this node is not expanded.
- */
- public void collapse()
- {
- isExpanded = false;
- }
-
- /**
- * Toggles the node state between collapsed and expanded, if the node
- * is expandable.
- */
- public void toggle()
- {
- if (isExpanded)
- {
- collapse();
- }
- else if (isExpandable())
- {
- expand();
- }
- }
-
- /**
- * Gets the proper node image for its current state, expanded or collapsed.
- * @return the current Image for node in its current state
- */
- public Image getImage()
- {
- return ((isExpanded && (expandedImage != null))
- ? expandedImage
- : collapsedImage);
- }
-
- /**
- * Sets the Image to use for this node when it is expanded.
- * @param image the image to use when this node is expanded
- * @see #setCollapsedImage
- * @see #getImage
- */
- public void setExpandedImage(Image image)
- {
- expandedImage = image;
- }
-
- /**
- * Sets the Image to use for this node when it is not expanded.
- * @param image the image to use when this node is collapsed
- * @see #setExpandedImage
- * @see #getImage
- */
- public void setCollapsedImage(Image image)
- {
- collapsedImage = image;
- }
-
- /**
- * Gets the current text label for this node.
- * @return the current text label for this node
- * @see #setText
- */
- public String getText()
- {
- return text;
- }
-
- /**
- * Sets a new text label for this node.
- * @param s the new text label for this node
- * @see #getText
- */
- public void setText(String s)
- {
- text = new String(s);
- }
-
- /**
- * Gets the object associated with this node.
- * @return the object associated with this node
- * @see #setDataObject
- */
- public Object getDataObject()
- {
- return dataObject;
- }
-
- /**
- * Sets an object to associate with this node.
- * @param theObject an object to associate with this node
- * @see #getDataObject
- */
- public void setDataObject(Object theObject)
- {
- dataObject = theObject;
- }
-
- /**
- * Gets the parent of this node.
- * @return this node's parent node
- * @see #getChild
- * @see #getSibling
- */
- public TreeNode getParent()
- {
- return parent;
- }
-
- /**
- * Gets the child of this node.
- * @return this node's child node
- * @see #getParent
- * @see #getSibling
- */
- public TreeNode getChild()
- {
- return child;
- }
-
- /**
- * Gets the sibling of this node.
- * @return this node's sibling node
- * @see #getChild
- * @see #getParent
- */
- public TreeNode getSibling()
- {
- return sibling;
- }
- }
-